#include "mainwindow.h"
#include <QMenuBar>
#include <QSettings>
#include <QTextEdit>
#include "settingsdialog.h"
#include <QFont>

Lightning::MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent)
{
    SetupActions();
    SetupMenus();
    SetupUI();
    QSettings settings;
    this->restoreGeometry(settings.value("mainwindow_geometry").toByteArray());
    this->restoreState(settings.value("mainwindow_windowstate").toByteArray());
}

Lightning::MainWindow::~MainWindow()
{
    QSettings settings;
    settings.setValue("mainwindow_geometry", this->saveGeometry());
    settings.setValue("mainwindow_windowstate", this->saveState());
}

void Lightning::MainWindow::SetupUI()
{
    this->tabs = new Lightning::TabBar(this);
    this->setCentralWidget(tabs);
}


void Lightning::MainWindow::SetupMenus()
{
    file_menu = menuBar()->addMenu("&File");
    file_menu->addAction(action_new);
    file_menu->addAction(action_open);
    file_menu->addAction(action_save);
    file_menu->addSeparator();
    file_menu->addAction(action_close);

    edit_menu = menuBar()->addMenu("&Edit");
    edit_menu->addAction(action_preferences);
}

void Lightning::MainWindow::SetupActions()
{
    this->action_new = new QAction(QIcon(":/images/file_new.png"), tr("&New"), this);
    action_new->setShortcuts(QKeySequence::New);
    action_new->setStatusTip(tr("Create a new file"));
    connect(action_new, SIGNAL(triggered()), this, SLOT(ActionNew()));

    this->action_save = new QAction(QIcon(":/images/file_save.png"), tr("&Save"), this);
    action_save->setShortcuts(QKeySequence::Save);
    action_save->setStatusTip(tr("Save current file"));
    connect(action_save, SIGNAL(triggered()), this, SLOT(ActionSave()));

    this->action_open = new QAction(QIcon(":/images/file_open.png"), tr("&Open"), this);
    action_open->setShortcuts(QKeySequence::Open);
    action_open->setStatusTip(tr("Open a file"));
    connect(action_open, SIGNAL(triggered()), this, SLOT(ActionOpen()));

    this->action_close = new QAction(QIcon(":/images/file_close.png"), tr("&Close"), this);
    action_close->setShortcuts(QKeySequence::Close);
    action_close->setStatusTip(tr("Close current file"));
    connect(action_close, SIGNAL(triggered()), this, SLOT(ActionClose()));

    this->action_preferences = new QAction(QIcon(":/images/edit_settings.png"), tr("&Preferences"), this);
    //action_preferences->setShortcuts(QKeySequence::Close);
    action_preferences->setStatusTip(tr("Display settings window"));
    connect(action_preferences, SIGNAL(triggered()), this, SLOT(ActionSettings()));
}

void Lightning::MainWindow::ActionSave()
{
#pragma message("Implement!")
}

void Lightning::MainWindow::ActionOpen()
{
#pragma message("Implement!")
}

void Lightning::MainWindow::ActionClose()
{
#pragma message("Implement!")
}

void Lightning::MainWindow::ActionNew()
{
    this->CreateEditor("new_file");
}

void Lightning::MainWindow::ActionSettings()
{
    SettingsDialog settingsdialog(this);
    int ret = settingsdialog.exec();
    if (ret == QDialog::Accepted)
    {
        // apply new settings
    }
}

/*
    Apply current settings to everyone
    WARNING: Only call this when settings change
 */
void Lightning::MainWindow::UpdateSettings()
{    
    // editor settings
    // font

    // color scheme
    // tabs
    // line numbers
    // highlight current line
}

/*
 */
int Lightning::MainWindow::CreateEditor(QString filename)
{
    this->editors.push_back(new Lightning::Editor());
    this->editors.last()->SetFilePath(filename);
    tabs->addTab(this->editors.last(), filename);
}
